package com.neuedu.oasystem.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.neuedu.oasystem.dao.IParticipantDao;
import com.neuedu.oasystem.dao.impl.ParticipantDaoImpl;
import com.neuedu.oasystem.entity.Participant;
import com.neuedu.oasystem.service.ParticipantService;
import com.neuedu.oasystem.util.DBHelper;

/**
 * Participant表实现增加 删（根据员工编号删除）， 改（根据会议编号修改参与会议人员，是否签到），
 * 查（根据会议编号查询，参与的会议的人；查询员工参与的会议）， 批量增加
 * 
 * @author admin
 *
 */
public class ParticipantServiceImpl implements ParticipantService {

	IParticipantDao participantDao;

	public ParticipantServiceImpl() {
		this.participantDao = new ParticipantDaoImpl();
	}

	/**
	 * 添加参与会议的人员
	 */
	@Override
	public boolean add(Participant param) {
		Connection connection = DBHelper.openConnection();
		try {
			participantDao.setConnection(connection);
			connection.setAutoCommit(false);
			boolean bool = participantDao.add(param);
			connection.commit();
			connection.setAutoCommit(false);
			return bool;
		} catch (SQLException e) {
			//e.printStackTrace();
			if (connection != null) {
				try {
					connection.rollback();
					connection.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}

			}
		}catch (RuntimeException e) {
			//e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, connection);
		}

		return false;
	}

	/**
	 * 根据会议编号修改参与会议人员，是否签到
	 */
	@Override
	public boolean edit(Participant param) {
		Connection connection = null;
		connection = DBHelper.openConnection();
		participantDao.setConnection(connection);
		try {
			connection.setAutoCommit(false);
			boolean bool = participantDao.edit(param);
			connection.commit();
			connection.setAutoCommit(true);
			return bool;
		} catch (SQLException e) {
			//e.printStackTrace();
			if (connection != null) {

				try {
					connection.rollback();
					connection.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		}catch (RuntimeException e) {
			//e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, connection);
		}

		return false;
	}

	/**
	 * 根据员工编号删除参与的会议
	 */
	@Override
	public boolean deleteById(int id) {
		Connection connection = null;
		connection = DBHelper.openConnection();
		participantDao.setConnection(connection);
		try {
			connection.setAutoCommit(false);
			boolean bool = participantDao.deleteById(id);
			connection.commit();
			connection.setAutoCommit(true);
			return bool;
		} catch (SQLException e) {
			e.printStackTrace();
			if (connection != null) {
				try {
					connection.rollback();
					connection.setAutoCommit(true);
				} catch (SQLException e1) {
					e1.printStackTrace();
				}
			}
		} finally {
			DBHelper.closeAll(null, null, connection);
		}

		return false;
	}

	/**
	 * 查询员工参与的会议
	 */
	@Override
	public Participant findById(int id) {
		Connection connection = null;
		Participant participant = null;
		connection = DBHelper.openConnection();
		participantDao.setConnection(connection);
		participant = participantDao.findById(id);

		return participant;
	}

	/**
	 * 查询全部
	 */
	@Override
	public List<Participant> findAll() {
		Connection connection = null;
		connection = DBHelper.openConnection();
		List<Participant> list = new ArrayList<Participant>();
		participantDao.setConnection(connection);
		try {
			list = participantDao.findAll();
		} catch (RuntimeException e) {
			//e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, connection);
		}
		return list;
	}

	/**
	 * 根据会议编号查询，参与的会议的人
	 */
	@Override
	public List<Participant> findByExample(Participant param) {
		Connection connection = null;
		List<Participant> list = new ArrayList<Participant>();
		connection = DBHelper.openConnection();
		participantDao.setConnection(connection);
		try {
			list = participantDao.findByExample(param);

		} catch (Exception e) {
			//e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, connection);
		}
		return list;
	}

	@Override
	public List<Participant> findByPager(int page, int size) {
		Connection connection = null;
		List<Participant> list = new ArrayList<Participant>();
		connection = DBHelper.openConnection();
		try {
			participantDao.setConnection(connection);
			list = participantDao.findByPager(page, size);
		} catch (RuntimeException e) {
			//e.printStackTrace();
		} finally {
			DBHelper.closeAll(null, null, connection);
		}
		return list;
	}

	@Override
	public int deleteByIds(int[] ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 批量新增参会人员
	 */
	@Override
	public int addAll(List<Participant> useparamrs) {
		int result = 0;
		Connection connection = DBHelper.openConnection();
		participantDao.setConnection(connection);
		try {
			connection.setAutoCommit(false);
			result = participantDao.addAll(useparamrs);
			connection.commit();
			connection.setAutoCommit(true);
		} catch (SQLException e) {
			// e.printStackTrace();
			if (connection != null) {
				try {
					connection.rollback();
					connection.setAutoCommit(true);
					return result;
				} catch (SQLException e1) {
					//e1.printStackTrace();
				}
			}
		} catch (RuntimeException e) {
			//e.printStackTrace();
		}
		return result;
	}

	@Override
	public int editAll(List<Participant> param) {
		return 0;
	}

}
